package com.zeng.plugin.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Dom4jUtil {
    private Document document;

    private SAXReader saxReader;


    public Dom4jUtil(String filePath) throws DocumentException {
        this();
        document = saxReader.read(filePath);

    }

    public Dom4jUtil() throws DocumentException {
        saxReader = new SAXReader();
    }
    public Dom4jUtil(URL url) throws DocumentException{
        this();
        document = saxReader.read(url);
    }
    public Dom4jUtil(InputStream inputStream) throws DocumentException{
        this();
        document = saxReader.read(inputStream);
    }

    public Element getRoot() {
        Element rootElement = document.getRootElement();
        return rootElement;
    }

    public  List<Element> getChildren(Element parent) {
        List<Element> children = new ArrayList<Element>();

        Iterator iterator = parent.elementIterator();
        while (iterator.hasNext()) {
            children.add((Element)( iterator.next()));
        }
        return children;
    }

//    public static void main(String[] args) throws DocumentException {
//
//        Dom4jUtil dom4jUtil = new Dom4jUtil("G:\\com.zhku.zeng\\plugin-test\\src\\main\\resource\\tables.xml");
//        Element root = dom4jUtil.getRoot();
//        dom4jUtil.getChildren(root);
//    }

}
